package com.wangzhou.head.servlet;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.wangzhou.common.util.EncryUtil;
import com.wangzhou.users.dao.IAAdminsDAO;
import com.wangzhou.users.dao.implay.AAdminsDAOImplay;
import com.wangzhou.users.pojo.AAdmins;

/**
 * 登陆的servlet,登陆前的操作
 * 
 * @author zjx
 *
 */
public class NoLoginServlet extends HttpServlet
{
	private IAAdminsDAO adminsDAO = new AAdminsDAOImplay() ; 
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		//让一个servlet有多个功能请求
		String method = request.getParameter("method");
		if("adminsLogin".equalsIgnoreCase(method))
		{
			this.adminsLogin(request,response);
		}else if("adminsLoginSubmit".equalsIgnoreCase(method))
		{
			this.adminsLoginSubmit(request,response);
		}
	}
	
	/**
	 * 打开管理员登陆页面
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void adminsLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String info = "" ; 
		String url = "/WEB-INF/back/adminsLogin.jsp" ;
		request.getRequestDispatcher(url).forward(request, response);
	}

	private void adminsLoginSubmit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String info = "" ; 
		String url = "/WEB-INF/back/adminsLogin.jsp" ; 
		//接收参数
		String email = request.getParameter("email");
		String password = request.getParameter("password");
		
		HttpSession session = request.getSession();
		//根据用户名查询管理员表
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("email", email);
		AAdmins admins = this.adminsDAO.findOne("2", paramsMap);
		if(admins != null)
		{
			//邮箱存在,用户存在
			if(admins.getPassword().equalsIgnoreCase(EncryUtil.getMD5(password)))
			{
				//密码正确
				if(admins.getStatus() == 1)
				{
					//账户启用登陆成功
					//将用户信息放到session中.在过滤器验证的时候使用
					//更新登陆次数,更新上次登陆时间
					session.setAttribute("admins", admins);
					
					//更新对象
					admins.setLasttime(new Date());
					admins.setLoginCount(admins.getLoginCount() + 1 );
					this.adminsDAO.update("1", admins);
					
					//登陆成功,客户端跳转
					response.sendRedirect(request.getContextPath() + "/back/usersServlet?method=main");
					return ;
				}else
				{
					info = "账户被禁用" ; 
				}
			}else
			{
				info = "密码不正确" ; 
			}
			request.setAttribute("admins", admins);
		}else
		{
			info = "邮箱未注册" ; 
		}
		request.setAttribute("info", info);
		request.getRequestDispatcher(url).forward(request, response);
	}
}
